<?php

class MHtmlLinkPager extends MHtmlElement {

    private $_url;
    private $_totalPages;
    private $_currentPage;
    private $_linkParam;

    public function __construct($url, $total, $current, $linkParam = 'p') {
        $this->_url = $url;
        $this->_totalPages = $total;
        $this->_currentPage = $current;
        $this->_linkParam = $linkParam;
    }

    public function render() {
        MPackage::import('mandrill-pager-css');
        
        $prev = $this->_currentPage - 1;
        $next = $this->_currentPage + 1;
        $n2l = $this->_totalPages - 1;
        $adj = 3;

        $pager = '';

        if ($this->_totalPages > 1) {
            $pagerContent = '';
            /* Previous Link */
            if ($this->_currentPage == 2) {
                $prevLink = MHtml::tag('a', array('href' => $this->_url, 'class' => 'ml-pager-item'), __t('mandrill.web.html.linkpager.previous'));
                $pagerContent .= $prevLink;
            } else if ($this->_currentPage > 2) {
                $prevLink = MHtml::tag('a', array('href' => $this->_url . '&' . $this->_linkParam . '=' . $prev, 'class' => 'ml-pager-item'), __t('mandrill.web.html.linkpager.previous'));
                $pagerContent .= $prevLink;
            }

            /* Pages Links */
            if ($this->_totalPages < 7 + ($adj * 2)) {
                $pagerContent .= ( $this->_currentPage == 1) ? MHtml::tag('span', array('class' => 'ml-pager-item-active'), '1') : MHtml::tag('a', array('class' => 'ml-pager-item', 'href' => $this->_url), '1');

                for ($i = 2; $i <= $this->_totalPages; $i++) {
                    if ($i == $this->_currentPage) {
                        $pagerContent .= MHtml::tag('span', array('class' => 'ml-pager-item-active'), $i);
                    } else {
                        $pagerContent .= MHtml::tag('a', array('class' => 'ml-pager-item', 'href' => $this->_url . '&' . $this->_linkParam . '=' . $i), $i);
                    }
                }
            } else {
                if ($this->_currentPage < 2 + ($adj * 2)) {
                    $pagerContent .= ( $this->_currentPage == 1) ? MHtml::tag('span', array('class' => 'ml-pager-item-active'), '1') : MHtml::tag('a', array('class' => 'ml-pager-item', 'href' => $this->_url), '1');

                    for ($i = 2; $i < 4 + ($adj * 2); $i++) {
                        if ($i == $this->_currentPage) {
                            $pagerContent .= MHtml::tag('span', array('class' => 'ml-pager-item-active'), $i);
                        } else {
                            $pagerContent .= MHtml::tag('a', array('class' => 'ml-pager-item', 'href' => $this->_url . '&' . $this->_linkParam . '=' . $i), $i);
                        }
                    }

                    $pagerContent .= ' ... ';
                    $pagerContent .= $pagerContent .= MHtml::tag('a', array('class' => 'ml-pager-item', 'href' => $this->_url . '&' . $this->_linkParam . '=' . $n2l), $n2l);
                    $pagerContent .= $pagerContent .= MHtml::tag('a', array('class' => 'ml-pager-item', 'href' => $this->_url . '&' . $this->_linkParam . '=' . $this->_totalPages), $this->_totalPages);
                } elseif ((($adj * 2) + 1 < $this->_currentPage) && ($this->_currentPage < $this->_totalPages - ($adj * 2))) {
                    $pagerContent .= MHtml::tag('a', array('class' => 'ml-pager-item', 'href' => $this->_url), '1');
                    $pagerContent .= MHtml::tag('a', array('class' => 'ml-pager-item', 'href' => $this->_url . '&' . $this->_linkParam . '=2'), '2');

                    $pagerContent .= ' ... ';

                    for ($i = $this->_currentPage - $adj; $i <= $this->_currentPage + $adj; $i++) {
                        if ($i == $this->_currentPage) {
                            $pagerContent .= MHtml::tag('span', array('class' => 'ml-pager-item-active'), $i);
                        } else {
                            $pagerContent .= MHtml::tag('a', array('class' => 'ml-pager-item', 'href' => $this->_url . '&' . $this->_linkParam . '=' . $i), $i);
                        }
                    }

                    $pagerContent .= ' ... ';
                    $pagerContent .= $pagerContent .= MHtml::tag('a', array('class' => 'ml-pager-item', 'href' => $this->_url . '&' . $this->_linkParam . '=' . $n2l), $n2l);
                    $pagerContent .= $pagerContent .= MHtml::tag('a', array('class' => 'ml-pager-item', 'href' => $this->_url . '&' . $this->_linkParam . '=' . $this->_totalPages), $this->_totalPages);
                } else {
                    $pagerContent .= MHtml::tag('a', array('class' => 'ml-pager-item', 'href' => $this->_url), '1');
                    $pagerContent .= MHtml::tag('a', array('class' => 'ml-pager-item', 'href' => $this->_url . '&' . $this->_linkParam . '=2'), '2');

                    $pagerContent .= ' ... ';

                    for ($i = $this->_totalPages - (2 + ($adj * 2)); $i <= $this->_totalPages; $i++) {
                        if ($i == $this->_currentPage) {
                            $pagerContent .= MHtml::tag('span', array('class' => 'ml-pager-item-active'), $i);
                        } else {
                            $pagerContent .= MHtml::tag('a', array('class' => 'ml-pager-item', 'href' => $this->_url . '&' . $this->_linkParam . '=' . $i), $i);
                        }
                    }
                }
            }

            /* Next Link */
            if ($this->_currentPage != $this->_totalPages){
                $pagerContent .= $prevLink = MHtml::tag('a', array('href' => $this->_url.'&'.$this->_linkParam.'='.$next, 'class' => 'ml-pager-item'), __t('mandrill.web.html.linkpager.next'));
            }

            $pager .= MHtml::tag('div', array('class' => 'ml-pager'), $pagerContent);
        } else {
            $item1 = MHtml::tag('span', array('class' => 'ml-pager-item-active'), $this->_currentPage);
            $pager .= MHtml::tag('div', array('class' => 'ml-pager'), $item1);
        }

        return $pager;
    }

    public function __toString() {
        return $this->render();
    }

}

?>
